#!/home/y/bin/perl

use FindBin qw/$Bin/;
use lib qw(lib ../lib);
use File::Which qw(which where);
use File::Temp qw/:POSIX/;

use Test::More;

my $qmgr = which('qmgr');
if ( !$qmgr ) {
    plan skip_all => 'No pbs client programs found!';
}
plan( 'no_plan' );

use_ok('Farsail::PBS');

Log::Log4perl->easy_init($Log::Log4perl::DEBUG);

my $pbs = Farsail::PBS->new();

isa_ok($pbs, 'Farsail::PBS');
$pbs->queue_name('batch');
is($pbs->queue_name, 'batch', 'set queue_name');
is($pbs->default_queue, get_default_queue(), 'default_queue');

$pbs->queue_name($pbs->default_queue);
my $maxjob = shift || 4;
my $job = $pbs->add_job('echo hello');
isa_ok($job, 'Farsail::PBS::Job', 'add_job');
my @jobs;
for ( 1..$maxjob ) {
    push @jobs, $pbs->add_job("echo this job $_", $job);
}
$pbs->add_job("sleep 1", \@jobs);
ok($pbs->check(), 'check');
ok($pbs->submit(1), 'submit');
ok($pbs->submit(), 'really submit');

sub get_default_queue {
    my $conf = `$qmgr -c 'print server default_queue'`;
    ($conf) = grep { !/^#/ } split /\n/, $conf;
    if ( $conf ) {
        return (split /\s*=\s*/, $conf)[1];
    }
}
